package com.web.servlet;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Alige on 2016/5/27.
 */
@Controller
@RequestMapping("/")
public class LoginMainServlet extends BaseController {


   

    @RequestMapping(value = "/home.do")
    public String home(HttpServletRequest request, ModelMap modelMap) {
//        Sys_user sys_user = securityContext.getSys_userInfo(Sys_user.class, request);
//        List<Sys_resourceModel> list = securityContext.getSys_user_resource(request);
//        String jsonArray = securityContext.getSys_resource2Tree(list);
//        List<Sys_roleModel> sys_roleModel = securityContext.getSys_user_role(request);
//        request.setAttribute("sys_user", sys_user);
//        if (null != sys_roleModel && sys_roleModel.size() > 0) {
//            request.setAttribute("sys_role", sys_roleModel.get(0).getRole_name());
//        }
//
//        School_info school_info =schoolService.findOne((long)sys_user.getOrg_id());
//        request.setAttribute("sys_roles", sys_roleModel);
//        modelMap.put("jsonArray", jsonArray);
//        modelMap.put("school_info", school_info);
//        modelMap.put("super_user", request.getParameter("super_user"));
        return "index";
    }


    @RequestMapping(value = "/clear_resource_cache.do")
    @ResponseBody
    public MessageBox clear_resource_cache(HttpServletRequest request, ModelMap modelMap) {
        MessageBox messageBox = null;
//        if (securityContext.clear_resource_cache()) {
//            messageBox = new MessageBox.Builder(0L).message("刷新成功！").build();
//        }
        return messageBox;
    }

    @RequestMapping(value = "/sys_Login.do", method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response) {
        try {
            String serverPath = request.getContextPath();
            request.getRequestDispatcher(serverPath + "/home.do");
            //response.sendRedirect(serverPath + "/home.do");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @RequestMapping(value = "/sys_logout.do", method = RequestMethod.GET)
    public void sys_logout(HttpServletRequest request, HttpServletResponse response) {

    }


    @RequestMapping(value = "/morris.do", method = RequestMethod.GET)
    public String morris(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        Set<String> keySet = map.keySet();
        Random rand = new Random();


        for (int i = 0; i < 24; i++) {
            int j = rand.nextInt(100); //生成0-100以内的随机数
            map.put("" + i, j);
        }
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        for (String str : keySet) {

            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("x", date + " " + str + ":00");
            map1.put("y", map.get(str));
            mapList.add(map1);
        }
        String pvJson = JSONObject.toJSONString(mapList);
        modelMap.put("pvJson", pvJson);
        return "smart/monitor";
    }

    protected String getJSessionId(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String jSessionVal = "";
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("JSESSIONID")) {
                    jSessionVal = cookie.getValue();
                    break;
                }
            }

        }
        return jSessionVal;
    }




    @RequestMapping("/orgList.do")
    @ResponseBody
    public Map<String, Object> orzList(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
     Map<String, Object> stringObjectMap = new HashMap<>();
     	
     	//版本号
     	
     
//        List<School_info> school_infoList = schoolService.find_orz_byType(0);
//        stringObjectMap.put("orgList", school_infoList);
        return stringObjectMap;
    }

    @RequestMapping("/orzList_sys.do")
    @ResponseBody
    public Map<String, Object> orzList_sys(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {
        Map<String, Object> stringObjectMap = new HashMap<>();
//        List<School_info> school_infoList = schoolService.find_orz_byType(1);
//        stringObjectMap.put("orgList", school_infoList);
        return stringObjectMap;
    }





}
